﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using UDS.Components;

namespace UDS.Entity
{
    public enum MailSortBy
    {
        Default,
        TimeSent,
        Sender,
        AttachSize,
        ClassName,
    }

    /// <summary>
    /// This class can be used for comparing 2 <see cref="MailInfo"/> instances.
    /// </summary>
    public class MailInfoComparer : IComparer<MailInfo>
    {
        public MailSortBy SortBy
        {
            get;
            private set;
        }

        public bool SortAsc
        {
            get;
            private set;
        }

        public MailInfoComparer(MailSortBy sortBy, bool sortAsc)
        {
            SortBy = sortBy;
            SortAsc = sortAsc;
        }

        public int Compare(MailInfo x, MailInfo y)
        {
            int value;
            if (x == null)
            {
                value = -1;
            }
            else if (y == null)
            {
                value = 1;
            }
            else
            {
                switch (SortBy)
                {
                    case MailSortBy.TimeSent:
                        value = x.TimeSent.CompareTo(y.TimeSent);
                        break;

                    case MailSortBy.Sender:
                        value = StringComparer.InvariantCultureIgnoreCase.Compare(x.Sender, y.Sender);
                        break;

                    case MailSortBy.AttachSize:
                        value = x.AttachSize.CompareTo(y.AttachSize);
                        break;

                    case MailSortBy.ClassName:
                        value = StringComparer.InvariantCultureIgnoreCase.Compare(x.ClassName, y.ClassName);
                        break;

                    case MailSortBy.Default:
                    default:
                        value = x.ID.CompareTo(y.ID);
                        break;
                }
            }

            return SortAsc ? value : (-value);
        }
    }

    /// <summary>
    /// Describe the berief information about a mail.
    /// </summary>
    public class MailInfo
    {
        #region properties
        public int ID
        {
            get;
            set;
        }

        public int ClassID
        {
            get;
            set;
        }

        public string ClassName
        {
            get;
            set;
        }

        public string Sender
        {
            get;
            set;
        }

        public DateTime TimeSent
        {
            get;
            set;
        }

        public string Subject
        {
            get;
            set;
        }

        public bool IsNew
        {
            get;
            set;
        }

        public string ReceiverList
        {
            get;
            set;
        }

        public int NumAttachFiles
        {
            get;
            set;
        }

        public int AttachSize
        {
            get;
            set;
        }
        #endregion

        #region methods

        internal static MailInfo ReadMailInfo(IDataReader dr)
        {
            MailInfo m = new MailInfo();
            m.ID = Convert.ToInt32(dr["MailID"]);
            m.ClassID = SQLUtil.ReadInt32(dr["ClassID"], -1);
            m.ClassName = Convert.ToString(dr["ClassName"]);
            m.Sender = Convert.ToString(dr["Sender"]);
            m.TimeSent = Convert.ToDateTime(dr["MailSendDate"]);
            m.Subject = Convert.ToString(dr["Subject"]);
            m.IsNew = !Convert.ToBoolean(dr["ReadFlag"]);
            m.ReceiverList = Convert.ToString(dr["TOList"]);
            m.NumAttachFiles = SQLUtil.ReadInt32(dr["NumAttachFiles"], 0);
            m.AttachSize = SQLUtil.ReadInt32(dr["AttachSize"], 0);

            return m;
        }
        #endregion
    }
}
